import math
import numpy as np
import matplotlib.pyplot as plt

# 设置Matplotlib后端为TkAgg
import matplotlib

matplotlib.use('TkAgg')

# 考生相关数据
A = 600  # 高考成绩
C = 100000  # 该省总考生人数
D = 0.6  # 试卷难度系数

# 高校 X 专业历年招生数据
A1, A2, A3, A4, A5 = 550, 560, 570, 580, 590  # 录取分数最低分
M1, M2, M3, M4, M5 = 650, 660, 670, 680, 690  # 录取分数最高分
G1, G2, G3, G4, G5 = 600, 610, 620, 630, 640  # 录取分数平均分
S1, S2, S3, S4, S5 = 50, 60, 70, 80, 90  # 录取人数
W1, W2, W3, W4, W5 = 2000, 1800, 1600, 1400, 1200  # 最低分数排名位次
Z1, Z2, Z3, Z4, Z5 = 90000, 92000, 94000, 96000, 98000  # 总考试人数
N1, N2, N3, N4, N5 = 0.5, 0.55, 0.6, 0.65, 0.7  # 历年难度系数

# 今年该专业计划录取人数
L = 100

def calculate_admission_probability(B):
    # 步骤 1：计算历年录取最低位次比例
    P1 = W1 / Z1
    P2 = W2 / Z2
    P3 = W3 / Z3
    P4 = W4 / Z4
    P5 = W5 / Z5

    # 步骤 2：计算平均录取最低位次比例
    P_avg = (P1 + P2 + P3 + P4 + P5) / 5

    # 步骤 3：估算今年可能的录取最低位次
    W_estimated = C * P_avg

    # 步骤 4：考虑试卷难度系数调整录取最低位次
    # 假设难度系数每变化 0.1，录取最低位次相应变化 100 名
    k = 100
    delta_N = [(D - N1), (D - N2), (D - N3), (D - N4), (D - N5)]
    W_adjusted = W_estimated + k * (sum(delta_N) / 5)

    # 步骤 5：计算录取概率
    # 计算招生计划变化对概率的影响因子
    S_avg = (S1 + S2 + S3 + S4 + S5) / 5
    r = (L - S_avg) / S_avg
    m = 0.1
    plan_factor = 1 / (1 + np.exp(-m * r))

    # 计算位次对概率的影响
    #distance =
    # 调整因子，用于控制曲线的陡峭程度
    steepness_factor = 5

    # 当 B 和 W_adjusted 相等时，让概率靠近 50%
    base_probability = 0.5

    # 综合计算录取概率
    # probability = base_probability + (position_factor - 0.5) * plan_factor

    probability = base_probability + (1 / (1 + np.exp(steepness_factor * (B - W_adjusted) / W_adjusted)) - 0.5) * plan_factor

    # 确保概率在 [0, 1] 范围内
    # probability = max(0, min(probability, 1))

    return probability

def sigmoid(x):
    a = 1.0  #  曲线的陡峭程度系数   a > 1  曲线变得更陡峭 a < 1  曲线变得更平 a=1 概率适中
    k = 1.0 # 截距 概率最大值1
    w = 500  # 阈值 推荐录取最低排名
    return k / (1 + np.exp(a*(x-w)))


sigmoid_inputs = np.arange(1, 1000, 10)  # 输入 1 - 1000 ，步长为10
sigmoid_outputs = sigmoid(sigmoid_inputs)
print("Sigmoid Function Input :: {}".format(sigmoid_inputs))
print("Sigmoid Function Output :: {}".format(sigmoid_outputs))

plt.plot(sigmoid_inputs, sigmoid_outputs)
plt.xlabel("Sigmoid Inputs")
plt.ylabel("Sigmoid Outputs")
plt.show()
